package v1

import (
	"gin-luban-server/global"
	"gin-luban-server/model"
	"gin-luban-server/model/request"
	"gin-luban-server/model/response"
	"gin-luban-server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)


// @Tags NginxPool
// @Summary 创建NginxPool
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.NginxPool true "创建NginxPool"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /nginx/pool/createNginxPool [post]
func CreateNginxPool(c *gin.Context) {
	var Pool model.NginxPool
	_ = c.ShouldBindJSON(&Pool)
	if err := service.CreateNginxPool(Pool); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags NginxPool
// @Summary 删除NginxPool
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.NginxPool true "删除NginxPool"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /nginx/pool/deleteNginxPool [delete]
func DeleteNginxPool(c *gin.Context) {
	var reqId request.GetById
	_ = c.ShouldBindJSON(&reqId)

	if err := service.DeleteNginxPool(reqId.Id); err != nil {
		global.GVA_LOG.Error("删除失败", zap.Any("error", err.Error()))
		response.FailWithMessage("删除失败" + err.Error(), c)

	} else {
		response.OkWithMessage("删除成功", c)
	}
}



//// @Tags NginxPool
//// @Summary 更新NginxPool
//// @Security ApiKeyAuth
//// @accept application/json
//// @Produce application/json
//// @Param data body model.NginxPool true "更新NginxPool"
//// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
//// @Router /nginx/pool/updateNginxPool [put]
func UpdateNginxPool(c *gin.Context) {
	var Pool model.NginxPool
	_ = c.ShouldBindJSON(&Pool)
	if err := service.UpdateNginxPool(&Pool); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags NginxPool
// @Summary 用id查询NginxPool
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.NginxPool true "用id查询NginxPool"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /nginx/pool/findNginxPool [get]
func GetNginxPoolById(c *gin.Context) {
	var reqId request.GetById
	_ = c.ShouldBind(&reqId)
	if err, rePool := service.GetNginxPoolById(reqId.Id); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"pool": rePool}, c)
	}
}
//
//// @Tags NginxPool
//// @Summary 分页获取NginxPool列表
//// @Security ApiKeyAuth
//// @accept application/json
//// @Produce application/json
//// @Param data body request.NginxPoolSearch true "分页获取NginxPool列表"
//// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
//// @Router /nginx/pool/getNginxPoolList [get]
func GetNginxPoolList(c *gin.Context) {
	var pageInfo request.NginxPoolSearch
	_ = c.ShouldBind(&pageInfo)
	authId := getUserAuthorityId(c)
	if err, list, total := service.GetNginxPoolInfoList(pageInfo, authId); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

